<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\home\controller;

use cmf\controller\RestBaseController;
use think\Db;
use think\Validate;
/**
 * @title 首页
 */
class IndexController extends RestBaseController
{
    /**
     * @title 首页
     * @description 接口说明
     * @author 开发者
     * @url /api/home/index/index
     * @method GET
     *
     * @param name:language type:string require:0 default: other desc:语言切换（英文传递此字段（en）,中文无需传递）
     *
     * @return rotation:轮播图@
     * @rotation id:轮播图id thumbnail:图片路径 thumbnail_mobile:移动图片路径  link:立即查看链接
     *
     * @return about:关于我们@!
     * @about id:关于我们id thumbnail:图片路径 introduce:简介
     *
     * @return type:我们的产品@
     * @type id:产品id thumbnail:图片路径 name:分类名称 children_name:分类子名称
     *
     * @return advantage:我们的优势@
     * @advantage id:优势id title:标题 introduce:简介
     *
     * @return contact:联系我们@!
     * @contact id:联系我们id address:地址 mobile:电话 phone:手机 wx:微信 email:邮箱
     */
    public function index(){
        $language = $this->request->param('language');
        $arr = [];

        //轮播图
        $res_rotation = CommonController::selectNoPositionData('rotation','id,thumbnail,thumbnail_mobile,link');
        $arr['rotation'] = $res_rotation;

        //关于我们
        if(isset($language) && !empty($language)){
            //英文
            $field_about = 'id,thumbnail,introduce_en introduce';
        }else{
            //中文
            $field_about = 'id,thumbnail,introduce';
        }
        $res_about = CommonController::findData('about',['id'=>1],$field_about);
        $arr['about'] = $res_about;

        //我们的产品（系列）
        if(isset($language) && !empty($language)){
            //英文
            $field_type = 'id,pid,thumbnail,name_en name';
            $flag = 'name';
        }else{
            //中文
            $field_type = 'id,pid,thumbnail,name';
            $flag = 'name';
        }
        $res_type = CommonController::getSeriesType($field_type,$flag);
        $arr['type'] = $res_type;

        //我们的优势
        if(isset($language) && !empty($language)){
            //英文
            $field_advantage = 'id,title_en title,introduce_en introduce';
        }else{
            //中文
            $field_advantage = 'id,title,introduce';
        }
        $res_advantage = CommonController::selectLimitData('advantage',$field_advantage,3);
        $arr['advantage'] = $res_advantage;

        //联系我们
        if(isset($language) && !empty($language)){
            //英文
            $field_contact = 'id,address_en address,mobile,phone,wx,email';
        }else{
            //中文
            $field_contact = 'id,address,mobile,phone,wx,email';
        }
        $res_contact = CommonController::findData('contact',['id'=>1],$field_contact);
        $arr['contact'] = $res_contact;

        $this->success('成功',$arr);
    }

    /**
     * @title 七牛云域名
     * @description 接口说明
     * @author 开发者
     * @url /api/home/index/qiNiuDomain
     * @method GET
     *
     * @return domain:七牛云域名
     */
    public function qiNiuDomain(){
        //七牛域名
        $arr['domain'] = CommonController::domain;
        $this->success('成功',$arr);
    }

    /**
     * @title 搜索产品系列（若没有数据提示：暂无数据，请联系客服）
     * @description 接口说明
     * @author 开发者
     * @url /api/home/index/searchSeriesList
     * @method GET
     *
     * @param name:language type:string require:0 default: other desc:语言切换（英文传递此字段（en）,中文无需传递）
     * @param name:keyword type:string require:1 default: other desc:关键字
     * @param name:page type:inter require:1 default: other desc:分页页码
     *
     * @return data:产品系列列表@
     * @data id:产品系列id thumbnail:图片路径 title:系列名称
     *
     * @return count_page:总页码
     */
    public function searchSeriesList(){
        $language = $this->request->param('language');
        $keyword = $this->request->param('keyword');
        $page = $this->request->param('page');//分页
        //验证
        $rule = config('site.search');
        $validate = new Validate($rule['rule'],$rule['msg']);
        if (!$validate->check(['keyword'=>$keyword,'page'=>$page])) {
            $this->error($validate->getError());
        }

        if(isset($language) && !empty($language)){
            //英文
            $field_series = 'id,thumbnail,title_en title';
            $where = ['title_en'=>['like','%'.$keyword.'%']];
        }else{
            //中文
            $field_series = 'id,thumbnail,title';
            $where = ['title'=>['like','%'.$keyword.'%']];
        }
        //查找二级分类
        $limit = CommonController::series_limit;
        $data = Db::name('series')
            ->where($where)->field($field_series)
            ->page($page,$limit)
            ->order('id desc')
            ->select()
            ->toArray();
        $count = Db::name('series')->where($where)->count();
        $count_page = ceil($count/$limit);
        $this->success('成功',['data'=>$data,'count_page'=>$count_page]);
    }

}
